
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 角色权限api
 */
 
@Api(tags = "RoleAuthorize",description = "角色权限")
@RestController
public class RoleAuthorizeController {
    
    @Autowired
    private RoleAuthorizeDao dao;
   
    /**
     * 获取角色权限集合
     * @return api结果
     */
    @ApiOperation(value = "获取角色权限集合")
    @RequestMapping(value =  "/api/RoleAuthorize/",method = {RequestMethod.GET})
    public ApiResult<List<RoleAuthorize>> Get(){
         ApiResult<List<RoleAuthorize>> result=new ApiResult<List<RoleAuthorize>>();
        try 
        {
            List<RoleAuthorize> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取权限集合
     * @param projectRoleId 项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目角色ID获取权限集合")
    @RequestMapping(value =  "/api/RoleAuthorize/ProjectRole/{projectRoleId}",method = {RequestMethod.GET})
    public ApiResult<List<RoleAuthorize>> Project(@PathVariable Long projectRoleId){
        ApiResult<List<RoleAuthorize>> result=new ApiResult<List<RoleAuthorize>>();
        try
        {
            List<RoleAuthorize> model= dao.findByProjectRoleId(projectRoleId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取角色权限信息
     * @param id 角色权限ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取角色权限信息")
    @RequestMapping(value =  "/api/RoleAuthorize/{id}",method = {RequestMethod.GET})
    public ApiResult<RoleAuthorize> Get(@PathVariable Long id){
        ApiResult<RoleAuthorize> result=new ApiResult<RoleAuthorize>();
        try
        {
            RoleAuthorize model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 保存角色权限
     * @param id 项目角色ID
     * @param model 角色权限信息
     * @return api结果
     */
    @ApiOperation(value = "保存角色权限")
    @RequestMapping(value =  "/api/RoleAuthorize/{projectRoleId}",method = {RequestMethod.PUT})
    public ApiResult<Object> Edit(@PathVariable Long projectRoleId,@RequestBody List<RoleAuthorize> model){
        ApiResult<Object> result=new ApiResult<Object>();
        try
        {
            dao.deleteByProjectRoleId(projectRoleId);

            for (RoleAuthorize roleAuthorize : model) {
                roleAuthorize.setCreateDate(new Date());
                roleAuthorize.setProjectRoleId(projectRoleId);
            }

            dao.saveAll(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加角色权限信息
     * @param model 角色权限信息
     * @return api结果
     */
    @ApiOperation(value = "添加角色权限信息")
    @RequestMapping(value =  "/api/RoleAuthorize/",method = {RequestMethod.POST})
    public ApiResult<RoleAuthorize> Add(@RequestBody RoleAuthorize model){
         ApiResult<RoleAuthorize> result=new ApiResult<RoleAuthorize>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除角色权限信息
     * @param id 角色权限ID
     * @return api结果
     */
    @ApiOperation(value = "删除角色权限信息")
    @RequestMapping(value =  "/api/RoleAuthorize/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

